//
//  GZLaunchIntroductionTool.m
//  GongZheng
//
//  Created by huan on 2018/4/27.
//  Copyright © 2018年 huan. All rights reserved.
//

#import "GZLaunchIntroductionTool.h"
#import "GZLaunchController.h"
#import "GZLoginController.h"

static NSString *const kAppVersion = @"appVersion";

@interface GZLaunchIntroductionTool ()

@property (nonatomic,strong) GZLaunchController *showVC;

@end

@implementation GZLaunchIntroductionTool


+ (instancetype)sharedIntroductionTool {
    //    static HOLaunchIntroductionTool *tool;
    //    static dispatch_once_t onceToken;
    //    dispatch_once(&onceToken, ^{
    //        tool = [[HOLaunchIntroductionTool alloc] init];
    //    });
    //    return tool;
    return [[GZLaunchIntroductionTool alloc] init];
}

- (void)introductionWithImages:(NSArray *)images finishMainViewController:(UIViewController *)finishMainViewController {
    if ([self isFirstLaunch]) {
        //创建对应的控制器
        self.showVC = [[GZLaunchController alloc] init];
        self.showVC.images = images;
        self.showVC.block = ^(){
            [[[[UIApplication sharedApplication] delegate] window] setRootViewController:finishMainViewController];
        };
        self.showVC.loginBlock = ^{
            [[[[UIApplication sharedApplication] delegate] window] setRootViewController:finishMainViewController];
            GZLoginController *loginVC = [[GZLoginController alloc] init];
            GZNavigationController *nav = [[GZNavigationController alloc] initWithRootViewController:loginVC];
            [finishMainViewController presentViewController:nav animated:YES completion:^{
                
            }];
        };
        [[[[UIApplication sharedApplication] delegate] window] setRootViewController:self.showVC];
    }else {
        // 已经运行过该版本
        [[[[UIApplication sharedApplication] delegate] window] setRootViewController:finishMainViewController];
    }
}

#pragma mark - 判断是不是首次登录或者版本更新
- (BOOL)isFirstLaunch{
    //获取当前版本号
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentAppVersion = infoDic[@"CFBundleShortVersionString"];
    //获取上次启动应用保存的appVersion
    NSString *version = UDObjectForKey(kAppVersion);
    //版本升级或首次登录
    if (version == nil || ![version isEqualToString:currentAppVersion]) {
        UDSetObjectForKey(currentAppVersion, kAppVersion);
        [USER_DEFAULT synchronize];
        return YES;
    }else{
        return NO;
    }
}


@end
